from models import *
import logging
import random
import string
from google.appengine.api import memcache

def generate_random_id(size=5):
    return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(size))

def create_admin(admin):
    q = DSAdmin(google_id=admin.user_id(), email=admin.email())
    q.put()
    return q

def create_admin_by_id(admin_id):
    q = DSAdmin(google_id=admin_id, email="none")  # was email="" and caused error - almog
    q.put()
    return q

def get_admin_by_googleid(google_id):        
    return DSAdmin.all().filter("google_id =", google_id).get()
  
def get_queue_by_cqid(queue_cqid):  
    if queue_cqid is None:
        return None
    queue = memcache.Client().get("queue?" + queue_cqid + ":obj") 
    if queue is None:
        queue = DSQueue.all().filter("queue_cqid = ", queue_cqid).get()
        if queue is not None:
            memcache.Client().set("queue?" + queue_cqid + ":obj", queue) 
    return queue

def get_positions_from_userid(user_cqid):
    positions = memcache.Client().get("user?" + str(user_cqid) + ":positions") 
    if positions is not None:
        return [get_position_from_posid(p) for p in positions]
    user = get_user_from_userid(user_cqid)
    if user is None:
        return []
    positions = DSPosition.all().filter("user_ref = ", user.key()).fetch(10)
    memcache.Client().set("user?" + user_cqid + ":positions", [p.pos_cqid for p in positions])
    return positions
        
def get_queue_from_pos(position):
    if position is None:
        return None
    return position.parent()

def get_queues_from_userid(user_cqid):
    positions = get_positions_from_userid(user_cqid)
    return [pos.parent() for pos in positions]

def get_queue_from_name(name, admin):
    admin = get_admin_by_googleid(admin.user_id())
    if admin is not None:
        return DSQueue.all().ancestor(admin).filter("name = ", name).fetch(None)
    return None

def get_queue_from_name_by_id(name, admin_id):
    admin = get_admin_by_googleid(admin_id)
    if admin is not None:
        return DSQueue.all().ancestor(admin).filter("name = ", name).fetch(None)
    return None

def get_user_from_userid(user_cqid):
    user = memcache.Client().get("user?" + user_cqid + ":obj")
    if user is None:
        user = DSUser.all().filter("user_cqid = ", user_cqid).get()
        if user is not None:
            memcache.Client().set("user?" + user.user_cqid + ":obj", user)
    return user

def create_user(details):
    name = details.get("name", "")
    phone = details.get("phone", "")  
    user = DSUser(user_cqid=generate_random_id(), name=name, phone=phone)
    user.put()
    memcache.Client().set("user?" + user.user_cqid + ":positions", [])
    memcache.Client().set("user?" + user.user_cqid + ":obj", user)
    return user

def get_position_from_posid(pos_cqid):
    if pos_cqid == None:
        return None
    pos = memcache.Client().get("position?" + pos_cqid + ":obj")
    if pos is None:
        pos = DSPosition.all().filter("pos_cqid = ", pos_cqid).get()
        if pos is not None:
            memcache.Client().set("position?" + pos.pos_cqid + ":obj", pos)
    return pos
    
def get_queue_from_posid(pos_cqid):
    pos = get_position_from_posid(pos_cqid)
    return get_queue_from_pos(pos)


